package com.solcoo.dao;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.solcoo.json.DingDanJson;
import com.solcoo.json.MonthStreamJson;
import com.solcoo.json.ReportByMerchantJson;
import com.solcoo.json.ReportByMerchantTypeJson;
import com.solcoo.json.ReportJson;
import com.solcoo.json.ReportTypeJson;
import com.solcoo.pojo.DingDan;
import com.solcoo.pojo.TempYhAndDd;

@Repository
public class DingDanDao {

	@Resource
	private SqlMapClient sqlMapClient;
	
    /**
     * 增加一条订单
     */
    private static final String INSERT_ORDER = "insertOrder";
    private static final String SELECT_ORDER_INFO = "selectOrderInfo";

    /**
     * 增加一条订单
     * @param dingdan
     * @throws SQLException
     */
    public void add(DingDan dingdan) throws SQLException {
    	sqlMapClient.insert(INSERT_ORDER, dingdan);
    }

    // 根据商户ID和个人ID查询订单列表，如果个人ID为0则查询该商户所有的订单，如果商户ID为0则查询该个人所有的订单
    public List<DingDan> queryList(int shanghuid, int gerenid, int youbiao) throws SQLException {
        Map<String, Object> param = new HashMap<String, Object>();
        if(shanghuid > 0)
        	param.put("shanghuid", shanghuid);
        if(gerenid > 0)
        	param.put("gerenid", gerenid);
        if(youbiao > 0)
        	param.put("youbiao", youbiao);
        return sqlMapClient.queryForList("selectOrderByIndex", param);
    }

    // 根据商户ID和个人ID查询（订单、个人）列表，如果个人ID为0则查询该商户所有的订单，如果商户ID为0则查询该个人所有的订单
    public List<TempYhAndDd> queryYDList(int shanghuid, int gerenid, int youbiao) throws SQLException {
    	Map<String, Object> param = new HashMap<String, Object>();
    	if(shanghuid > 0)
        	param.put("shanghuid", shanghuid);
        if(gerenid > 0)
        	param.put("gerenid", gerenid);
        if(youbiao > 0)
        	param.put("youbiao", youbiao);
        return sqlMapClient.queryForList("selectYDByIndex", param);
    }

    // 根据商户ID和个人ID查询（订单、个人）列表，如果个人ID为0则查询该商户所有的订单，如果商户ID为0则查询该个人所有的订单
    public List<TempYhAndDd> queryYDListByDate(Map<String, Object> param) throws SQLException {
        return sqlMapClient.queryForList("selectYDByIndexByDate", param);
    }

	public DingDan queryObject(int id) throws SQLException {
    	return (DingDan)sqlMapClient.queryForObject("selectOrderById", id);
	}

    public List<DingDan> queryAllList(int shanghuid, int gerenid) throws Exception {
    	Map<String, Object> param = new HashMap<String, Object>();
    	param.put("shanghuid", shanghuid);
        if(gerenid > 0)
        	param.put("gerenid", gerenid);
        return sqlMapClient.queryForList("selectAllOrderByIndex", param);
    }

    public List<DingDan> queryAllList(Map<String, Object> param) throws SQLException {
        return sqlMapClient.queryForList(SELECT_ORDER_INFO,param);
    }

	public boolean deleteObjectById(int id) throws SQLException {
		return sqlMapClient.update("updateOrderDelFlagById", id) == 1 ? true : false;
	}

	// 修改订单查看状态
	public void updateDDNQCount(Map<String, Object> param) throws SQLException {
		sqlMapClient.update("updateDDNQCount", param);
	}

	/**
	 * 获取年节省金额
	 */
	public Double getYSaveMoney(Map<String, Object> param) throws SQLException {
		return (Double) sqlMapClient.queryForObject("getYSaveMoney", param);
	}
	
	/**
	 *  用户获取月订单流水
	 */
	public List<MonthStreamJson> getMonthStream(Map<String, Object> param) throws SQLException {
		return sqlMapClient.queryForList("getMonthStream", param);
	}
	
	/**
	 *  用户获取日订单流水
	 */
	public List<DingDanJson> getDayStream(Map<String, Object> param) throws SQLException {
		return sqlMapClient.queryForList("getDayStream", param);
	}
	
	/**
	 * 获取商家支出报表
	 */
	public List<ReportByMerchantJson> getReportByMerchant(Map<String, Object> param) throws SQLException {
		return sqlMapClient.queryForList("getReportByMerchant", param);
	}
	
	/**
	 * 获取商家支出报表明细
	 */
	public List<DingDanJson> getReportmxByMerchant(Map<String, Object> param) throws SQLException {
		return sqlMapClient.queryForList("getReportmxByMerchant", param);
	}
	
	/**
	 * 获取类型支出报表
	 */
	public List<ReportByMerchantTypeJson> getReportByMerchantType(Map<String, Object> param) throws SQLException {
		return sqlMapClient.queryForList("getReportByMerchantType", param);
	}
	
	/**
	 * 获取类型支出报表明细
	 */
	public List<DingDanJson> getReportmxByMerchantType(Map<String, Object> param) throws SQLException {
		return sqlMapClient.queryForList("getReportmxByMerchantType", param);
	}
	
	public void updateRevokeemp(Map<String, Object> param) throws SQLException {
		sqlMapClient.update("updateRevokeemp", param);
	}
	
	/**
	 * 获取商户报表
	 */
	public List<ReportJson> getReport(Map<String, Object> param) throws SQLException {
		return sqlMapClient.queryForList("getReport", param);
	}
	
	/**
	 * 获取商户类型报表
	 */
	public List<ReportTypeJson> getReportByType(Map<String, Object> param) throws SQLException {
		return sqlMapClient.queryForList("getReportByType", param);
	}

	/**
	 * 获取月节省金额
	 */
	public Double getMSaveMoney(int gerenid, String ym) throws SQLException {
		Map<String, Object> param = new HashMap<String, Object>();
		
		param.put("gerenid", gerenid);
		param.put("ym", ym);
		
		return (Double) sqlMapClient.queryForObject("getMSaveMoney", param);
	}
	
}
